home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacOS 8 Resources / Developer Tools / Mac OS 8 Interfaces & Libraries / Interfaces / CIncludes / Threads.h < prev    next >
C/C++ Source or Header  |  1996-05-01  |  9KB  |  236 lines

  1. /*
  2.      File:        Threads.h
  3.  
  4.      Contains:    Thread Manager Interfaces.
  5.  
  6.      Version:    Technology:    System 7.5
  7.                  Release:    Universal Interfaces 3.0d3 on Copland DR1
  8.  
  9.      Copyright:    © 1984-1996 by Apple Computer, Inc.  All rights reserved.
  10.  
  11.      Bugs?:        If you find a problem with this file, send the file and version
  12.                  information (from above) and the problem description to:
  13.  
  14.                      Internet:    apple.bugs@applelink.apple.com
  15.                      AppleLink:    APPLE.BUGS
  16.  
  17. */
  18. #ifndef __THREADS__
  19. #define __THREADS__
  20.  
  21. #ifndef __ERRORS__
  22. #include <Errors.h>
  23. #endif
  24. #ifndef __MEMORY__
  25. #include <Memory.h>
  26. #endif
  27.  
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31.  
  32. #if PRAGMA_IMPORT_SUPPORTED
  33. #pragma import on
  34. #endif
  35.  
  36. #if PRAGMA_ALIGN_SUPPORTED
  37. #pragma options align=mac68k
  38. #endif
  39.  
  40. #if FOR_SYSTEM7_AND_SYSTEM8_COOPERATIVE
  41. /* Thread states */
  42. typedef unsigned short ThreadState;
  43.  
  44. enum {
  45.     kReadyThreadState            = 0,
  46.     kStoppedThreadState            = 1,
  47.     kRunningThreadState            = 2
  48. };
  49.  
  50. /* Error codes have been meoved to Errors.(pah) */
  51. /* Thread environment characteristics */
  52. typedef void *ThreadTaskRef;
  53. /* Thread characteristics */
  54. typedef unsigned long ThreadStyle;
  55.  
  56. enum {
  57.     kCooperativeThread            = 1L << 0,
  58.     kPreemptiveThread            = 1L << 1
  59. };
  60.  
  61. /* Thread identifiers */
  62. typedef unsigned long ThreadID;
  63.  
  64. enum {
  65.     kNoThreadID                    = 0,
  66.     kCurrentThreadID            = 1,
  67.     kApplicationThreadID        = 2
  68. };
  69.  
  70. /* Options when creating a thread */
  71. typedef unsigned long ThreadOptions;
  72.  
  73. enum {
  74.     kNewSuspend                    = (1 << 0),
  75.     kUsePremadeThread            = (1 << 1),
  76.     kCreateIfNeeded                = (1 << 2),
  77.     kFPUNotNeeded                = (1 << 3),
  78.     kExactMatchThread            = (1 << 4)
  79. };
  80.  
  81. /* Information supplied to the custom scheduler */
  82. struct SchedulerInfoRec {
  83.     unsigned long                     InfoRecSize;
  84.     ThreadID                         CurrentThreadID;
  85.     ThreadID                         SuggestedThreadID;
  86.     ThreadID                         InterruptedCoopThreadID;
  87. };
  88. typedef struct SchedulerInfoRec SchedulerInfoRec;
  89.  
  90. typedef SchedulerInfoRec *SchedulerInfoRecPtr;
  91. #if GENERATING68K && GENERATINGCFM
  92. /*
  93.     The following UniversalProcPtrs are for CFM-68k compatiblity with
  94.     the implementation of the Thread Manager.
  95. */
  96. typedef UniversalProcPtr ThreadEntryProcPtr;
  97. typedef UniversalProcPtr ThreadSchedulerProcPtr;
  98. typedef UniversalProcPtr ThreadSwitchProcPtr;
  99. typedef UniversalProcPtr ThreadTerminationProcPtr;
  100. typedef UniversalProcPtr DebuggerNewThreadProcPtr;
  101. typedef UniversalProcPtr DebuggerDisposeThreadProcPtr;
  102. typedef UniversalProcPtr DebuggerThreadSchedulerProcPtr;
  103. enum {
  104.     uppThreadEntryProcInfo = kPascalStackBased
  105.          | RESULT_SIZE(SIZE_CODE(sizeof(void*)))
  106.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(void*))),
  107.     uppThreadSchedulerProcInfo = kPascalStackBased
  108.          | RESULT_SIZE(SIZE_CODE(sizeof(ThreadID)))
  109.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(SchedulerInfoRecPtr))),
  110.     uppThreadSwitchProcInfo = kPascalStackBased
  111.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID)))
  112.          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(void*))),
  113.     uppThreadTerminationProcInfo = kPascalStackBased
  114.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID)))
  115.          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(void*))),
  116.     uppDebuggerNewThreadProcInfo = kPascalStackBased
  117.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID))),
  118.     uppDebuggerDisposeThreadProcInfo = kPascalStackBased
  119.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID))),
  120.     uppDebuggerThreadSchedulerProcInfo = kPascalStackBased
  121.          | RESULT_SIZE(SIZE_CODE(sizeof(ThreadID)))
  122.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(SchedulerInfoRecPtr)))
  123. };
  124. #define NewThreadEntryProc(userRoutine)                (ThreadEntryProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadEntryProcInfo, GetCurrentArchitecture())
  125. #define NewThreadSchedulerProc(userRoutine)            (ThreadSchedulerProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadSchedulerProcInfo, GetCurrentArchitecture())
  126. #define NewThreadSwitchProc(userRoutine)            (ThreadSwitchProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadSwitchProcInfo, GetCurrentArchitecture())
  127. #define NewThreadTerminationProc(userRoutine)        (ThreadTerminationProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadTerminationProcInfo, GetCurrentArchitecture())
  128. #define NewDebuggerNewThreadProc(userRoutine)        (DebuggerNewThreadProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppDebuggerNewThreadProcInfo, GetCurrentArchitecture())
  129. #define NewDebuggerDisposeThreadProc(userRoutine)    (DebuggerDisposeThreadProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppDebuggerDisposeThreadProcInfo, GetCurrentArchitecture())
  130. #define NewDebuggerThreadSchedulerProc(userRoutine)    (DebuggerThreadSchedulerProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppDebuggerThreadSchedulerProcInfo, GetCurrentArchitecture())
  131. #else
  132. /*
  133.     The following ProcPtrs cannot be interchanged with UniversalProcPtrs because
  134.     of differences between 680x0 and PowerPC runtime architectures with regard to
  135.     the implementation of the Thread Manager.
  136. */
  137. /* Prototype for thread's entry (main) routine */
  138. typedef void *voidPtr;
  139. typedef pascal voidPtr (*ThreadEntryProcPtr)(void *threadParam);
  140. /* Prototype for custom thread scheduler routine */
  141. typedef pascal ThreadID (*ThreadSchedulerProcPtr)(SchedulerInfoRecPtr schedulerInfo);
  142. /* Prototype for custom thread switcher routine */
  143. typedef pascal void (*ThreadSwitchProcPtr)(ThreadID threadBeingSwitched, void *switchProcParam);
  144. /* Prototype for thread termination notification routine */
  145. typedef pascal void (*ThreadTerminationProcPtr)(ThreadID threadTerminated, void *terminationProcParam);
  146. /* Prototype for debugger NewThread notification */
  147. typedef pascal void (*DebuggerNewThreadProcPtr)(ThreadID threadCreated);
  148. /* Prototype for debugger DisposeThread notification */
  149. typedef pascal void (*DebuggerDisposeThreadProcPtr)(ThreadID threadDeleted);
  150. /* Prototype for debugger schedule notification */
  151. typedef pascal ThreadID (*DebuggerThreadSchedulerProcPtr)(SchedulerInfoRecPtr schedulerInfo);
  152. #endif
  153. /* Thread Manager routines */
  154. extern pascal OSErr CreateThreadPool(ThreadStyle threadStyle, short numToCreate, Size stackSize)
  155.  THREEWORDINLINE(0x303C, 0x0501, 0xABF2);
  156.  
  157. extern pascal OSErr GetFreeThreadCount(ThreadStyle threadStyle, short *freeCount)
  158.  THREEWORDINLINE(0x303C, 0x0402, 0xABF2);
  159.  
  160. extern pascal OSErr GetSpecificFreeThreadCount(ThreadStyle threadStyle, Size stackSize, short *freeCount)
  161.  THREEWORDINLINE(0x303C, 0x0615, 0xABF2);
  162.  
  163. extern pascal OSErr GetDefaultThreadStackSize(ThreadStyle threadStyle, Size *stackSize)
  164.  THREEWORDINLINE(0x303C, 0x0413, 0xABF2);
  165.  
  166. extern pascal OSErr ThreadCurrentStackSpace(ThreadID thread, unsigned long *freeStack)
  167.  THREEWORDINLINE(0x303C, 0x0414, 0xABF2);
  168.  
  169. extern pascal OSErr NewThread(ThreadStyle threadStyle, ThreadEntryProcPtr threadEntry, void *threadParam, Size stackSize, ThreadOptions options, void **threadResult, ThreadID *threadMade)
  170.  THREEWORDINLINE(0x303C, 0x0E03, 0xABF2);
  171.  
  172. extern pascal OSErr DisposeThread(ThreadID threadToDump, void *threadResult, Boolean recycleThread)
  173.  THREEWORDINLINE(0x303C, 0x0504, 0xABF2);
  174.  
  175. extern pascal OSErr YieldToThread(ThreadID suggestedThread)
  176.  THREEWORDINLINE(0x303C, 0x0205, 0xABF2);
  177.  
  178. extern pascal OSErr YieldToAnyThread(void )
  179.  FOURWORDINLINE(0x42A7, 0x303C, 0x0205, 0xABF2);
  180.  
  181. extern pascal OSErr GetCurrentThread(ThreadID *currentThreadID)
  182.  THREEWORDINLINE(0x303C, 0x0206, 0xABF2);
  183.  
  184. extern pascal OSErr GetThreadState(ThreadID threadToGet, ThreadState *threadState)
  185.  THREEWORDINLINE(0x303C, 0x0407, 0xABF2);
  186.  
  187. extern pascal OSErr SetThreadState(ThreadID threadToSet, ThreadState newState, ThreadID suggestedThread)
  188.  THREEWORDINLINE(0x303C, 0x0508, 0xABF2);
  189.  
  190. extern pascal OSErr SetThreadStateEndCritical(ThreadID threadToSet, ThreadState newState, ThreadID suggestedThread)
  191.  THREEWORDINLINE(0x303C, 0x0512, 0xABF2);
  192.  
  193. extern pascal OSErr SetThreadScheduler(ThreadSchedulerProcPtr threadScheduler)
  194.  THREEWORDINLINE(0x303C, 0x0209, 0xABF2);
  195.  
  196. extern pascal OSErr SetThreadSwitcher(ThreadID thread, ThreadSwitchProcPtr threadSwitcher, void *switchProcParam, Boolean inOrOut)
  197.  THREEWORDINLINE(0x303C, 0x070A, 0xABF2);
  198.  
  199. extern pascal OSErr SetThreadTerminator(ThreadID thread, ThreadTerminationProcPtr threadTerminator, void *terminationProcParam)
  200.  THREEWORDINLINE(0x303C, 0x0611, 0xABF2);
  201.  
  202. extern pascal OSErr ThreadBeginCritical(void )
  203.  THREEWORDINLINE(0x303C, 0x000B, 0xABF2);
  204.  
  205. extern pascal OSErr ThreadEndCritical(void )
  206.  THREEWORDINLINE(0x303C, 0x000C, 0xABF2);
  207.  
  208. extern pascal OSErr SetDebuggerNotificationProcs(DebuggerNewThreadProcPtr notifyNewThread, DebuggerDisposeThreadProcPtr notifyDisposeThread, DebuggerThreadSchedulerProcPtr notifyThreadScheduler)
  209.  THREEWORDINLINE(0x303C, 0x060D, 0xABF2);
  210.  
  211. extern pascal OSErr GetThreadCurrentTaskRef(ThreadTaskRef *threadTRef)
  212.  THREEWORDINLINE(0x303C, 0x020E, 0xABF2);
  213.  
  214. extern pascal OSErr GetThreadStateGivenTaskRef(ThreadTaskRef threadTRef, ThreadID threadToGet, ThreadState *threadState)
  215.  THREEWORDINLINE(0x303C, 0x060F, 0xABF2);
  216.  
  217. extern pascal OSErr SetThreadReadyGivenTaskRef(ThreadTaskRef threadTRef, ThreadID threadToSet)
  218.  THREEWORDINLINE(0x303C, 0x0410, 0xABF2);
  219.  
  220. #endif
  221.  
  222. #if PRAGMA_ALIGN_SUPPORTED
  223. #pragma options align=reset
  224. #endif
  225.  
  226. #if PRAGMA_IMPORT_SUPPORTED
  227. #pragma import off
  228. #endif
  229.  
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233.  
  234. #endif /* __THREADS__ */
  235.  
  236.